246D - Colorful Graph - CodeForces Solution


brute force dfs and similar graphs *1600

Please click on ads to support us..

Python Code:

from collections import defaultdict


def solve(n,colors,edges):
    graph=defaultdict(set)
    for u,v in edges:
        u-=1
        v-=1
        graph[colors[u]].add(colors[v])
        graph[colors[v]].add(colors[u])
    
    for i in range(n):
        graph[colors[i]].add(colors[i])
    
    ans=None
    curr=0
    for k,v in graph.items():
        if len(v)>curr:
            curr=len(v)
            ans=k
        elif len(v)==curr:
            ans=min(ans,k)
    print(ans)




n,m=map(int,input().split())
colors=list(map(int,input().split()))
edges=[]
for i in range(m):
    u,v=map(int,input().split())
    edges.append((u,v))

solve(n,colors,edges)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define pb push_back
#define vlli vector<long long int>
typedef long long int lli;
typedef unsigned long long int ulli;
#define vvlli vector<vector<lli> >
#define vvp vector<vector<pair<lli,lli> > >


int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lli t;
    t=1;
    // cin>>t;
    for(lli ncase=1;ncase<=t;++ncase){
        // cout<<"Case #"<<ncase<<": ";
        
        lli n,m;
        cin>>n>>m;

        lli a[n+1];

        vvlli c(100005);

        for(lli i=1;i<=n;++i){
            cin>>a[i];
            c[a[i]].push_back(i);
        }

        vvlli v(n+1);

        for(lli i=0;i<m;++i){
            lli x, y;
            cin>>x>>y;
            v[x].push_back(y);
            v[y].push_back(x);
        }

        map<lli, lli>done;

        lli vis[100005];
        memset(vis, 0, sizeof(vis));

        lli maxi=-1;
        lli ind=0;

        for(lli i=0;i<100005;++i){
            lli ans=0;
            for(auto x:c[i]){
                for(auto y:v[x]){
                    if(a[y]!=i && !vis[a[y]]){
                        ans++;
                        vis[a[y]]=1;
                    }
                }        
            }
            for(auto x:c[i]){
                for(auto y:v[x]){
                    vis[a[y]]=0;
                }        
            }
            if(c[i].size()==0)continue;
            if(maxi<ans){
                // cout<<i<<' '<<ans<<'\n';
                maxi=ans;
                ind=i;
            }
            
        }

        cout<<ind<<'\n';
        
    }
}


Comments

Submit
0 Comments
More Questions

507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm